<?php
namespace Home\Controller;
use Home\Controller\BaseController;

class CategoryController extends BaseController {

    public function _initialize() {
        parent::_initialize();
    }

    //商品分类展示页
    public function index() {
        $id = I('get.id');
        $str = I('str');
        $end = I('end');
        if(!empty($end)){
            $where['goods_price'] = array('between',array($str,$end));
        }
        $this->assign('strs', $str);
        $this->assign('end', $end);
        
        
        $type = I('get.type');
        $order = I('get.order');
        if (empty($order)) {
            $order = 'desc';
        }

        if ($order == 'desc') {
            $o = "asc";
        } else {
            $o = 'desc';
        }

        $category = M('Category');
        $grade = $category->where(array('cat_id' => $id))->field('grade,cat_name')->find();
        $this->assign('cat_name', $grade['cat_name']);
        switch ($grade['grade']) {
            case 1:
                $where['goods_class1'] = $id;
                break;
            case 2:
                $where['goods_class2'] = $id;
                break;
            case 3:
                $where['goods_class3'] = $id;
                break;
            default:
                $where['goods_class'] = $id;
        }

        switch ($type) {
            case 'sales':
                $orders = 'goods_is_index desc,goods_realsale desc';
                break;
            case 'price':
                $orders = 'goods_is_index desc,goods_price ' . $order;
                break;
            case 'hits':
                $orders = 'goods_is_index desc,goods_hits desc';
                break;
            default:
                $orders = 'goods_is_index desc,goods_created desc';
        }

        $this->assign('str', $type);
        $this->assign('id', $id);
        $this->assign('order', $o);

        $goods = M('Goods');
        $where['goods_sale'] = 1;
        $where['goods_state'] = 1;
        $count = $goods->where($where)->count(); // 查询满足要求的总记录数
        $this->assign('count', $count);
        $Page = new \Think\Page($count, 12); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show(); // 分页显示输出
        $arr = $goods->where($where)->order($orders)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('arr', $arr);
        $this->assign('page', $show);

        //推荐产品
        $put['goods_sale'] = 1;
        $put['goods_state'] = 1;
        $put['goods_class'] = $id;
        $putGoods = $goods->where($put)->limit(3)->select();
        $this->assign('putGoods', $putGoods);
        $category = M('category')->where("cat_id = $id")->field('cat_id,cat_name,parent_id,grade')->find();
        switch ($category['grade']) {
            case 1:
                $this->assign('category_name1', $category['cat_name']);
                $this->assign('level', 1);
                break;
            case 2:
                $class1_name = M('category')->where(array('cat_id' => $category['parent_id']))->field('cat_id,cat_name,parent_id,grade')->find();
                $class1 = M('category')->where(array('parent_id' => $class1_name['cat_id']))->select();

                $this->assign('class1', $class1);
                $this->assign('category_name1', $class1_name['cat_name']);
                $this->assign('category_name2', $category['cat_name']);
                $this->assign('level', 2);
                break;
            case 3:
                $class2_name = M('category')->where(array('cat_id' => $category['parent_id']))->field('cat_id,cat_name,parent_id,grade')->find();
                $class2 = M('category')->where(array('parent_id' => $class2_name['cat_id']))->select();

                $class1_name = M('category')->where(array('cat_id' => $class2_name['parent_id']))->field('cat_id,cat_name,parent_id,grade')->find();
                $class1 = M('category')->where(array('parent_id' => $class1_name['cat_id']))->select();

                $this->assign('class1', $class1);
                $this->assign('class2', $class2);
                $this->assign('category_name1', $class1_name['cat_name']);
                $this->assign('category_name2', $class2_name['cat_name']);
                $this->assign('category_name3', $category['cat_name']);
                $this->assign('level', 3);
                break;
        }
        
        //获取当前分类的筛选
        $selectCat = M('category')->where("parent_id = $id")->select();
        $this->assign('selectCat', $selectCat);
        
        
        
        
        $this->display();
    }

}
